//****************************************************************************************
//*
//* File Name: uart_buffer.c 
//* Level: bsp_UART
//* Version: v4.0
//* Date: 2024-07-09 13:05:10
//* Author: su
//* 
//****************************************************************************************/

//******************************** Includes *********************************//
#include "uart_buffer.h"
#include <string.h>
#include <stdint.h>
//******************************** code *********************************//
static uint8_t Serial_number_one_buffer[BUFFSIZEMAX]; //buff1
static uint8_t Serial_number_two_buffer[BUFFSIZEMAX]; //buff2
static uint8_t Serial_number_three_buffer[BUFFSIZEMAX]; //buff3

static uartdata_t uartdatahandler; //发过来的数据包
static sendpack_t sendpackhandler; //拆包后的数据包 

//数组集合
static uint8_t *buffer_gather[] = {Serial_number_one_buffer, Serial_number_two_buffer, Serial_number_three_buffer};
#define SIZE_BUFFER_GATHER (sizeof(buffer_gather) / sizeof(uint8_t *))  //计算有几个数组

uint8_t bufferinit(void) 
{
    uint8_t *getaddress;
    getaddress = get_nextbuffer_address();
    uartdatahandler.start_byte      = &getaddress[0];
    uartdatahandler.payloadBuffer   = getaddress;
    uartdatahandler.end_byte        = &getaddress[BUFFSIZEMAX];
	
	return 0;
}
//内部handler总控结构体
uartdata_t *getbuffer(void)
{
    return &uartdatahandler;
}



uint8_t *get_nextbuffer_address(void) {
    static uint8_t current_buffer_index = 0;
    uint8_t *next_buffer = 0;

   
    for (int i = 0; i < SIZE_BUFFER_GATHER; i++) {
        if (current_buffer_index == i) { 
            next_buffer = buffer_gather[(i + 1) % SIZE_BUFFER_GATHER]; 
            current_buffer_index = (i + 1) % SIZE_BUFFER_GATHER; 
            break; 
        }
    }

    return next_buffer; 
}
//uint8_t *get_nextbuffer_address(void)
//{
//    if(1 == addresscount)
//    {
//        addresscount++;
//        return Serial_number_one_buffer;
//    }
//    else if (2 == addresscount)
//    {
//        addresscount++;
//        return Serial_number_two_buffer;
//    }
//    else if(3 == addresscount)
//    {
//        addresscount = 1;
//        return Serial_number_three_buffer;
//    }
//    else
//    {
//        /* code */  
//        return 0;     
//    }
//}

uint8_t calculateXOR(uint32_t* data, uint8_t length) {
    uint8_t xorValue = 0;
    for(uint8_t i = 0; i < length; i++) {
        xorValue ^= data[i];
    }
    return xorValue;
}
uint8_t receive_header_frame(uartdata_t *uartdata) //解包函数
{
	uartdata->start_byte = &uartdata->payloadBuffer[0];
	uartdata->end_byte   = &uartdata->payloadBuffer[uartdata->length-1];
	if(HEADBYTE != *uartdata->start_byte)
	{
		return DATASTARTERROR;
	}
	if(ENDBYTE != *uartdata->end_byte)
	{
		return DATAENDERROR;
	}
	uint8_t tempinspection = 0;
	for(uint8_t i = 1;i < uartdata->length - 2; i++)
	{
		
		tempinspection ^= uartdata->payloadBuffer[i];
		
	}
	if(tempinspection != uartdata->payloadBuffer[uartdata->length-2])
	{
		return XORERROR;
	}
	return DATAOK;
}

sendpack_t *parsingpack(uartdata_t *uartdata) //拆包函数
{
	sendpackhandler.length = uartdata->length - 3;
	sendpackhandler.Buffer_address = &uartdata->payloadBuffer[1];
	memset(&sendpackhandler.Buffer_address[uartdata->length - 2],0,uartdata->length - (uartdata->length - 2));
	return &sendpackhandler;
}

